package jsu.ask.controller;

import jsu.ask.pojo.Manager;
import jsu.ask.pojo.User;
import jsu.ask.service.ManagerService;
import jsu.ask.service.UserService;
import jsu.ask.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    //controller 调 service层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("ManagerServiceImpl")
    private ManagerService managerService;

    //后台管理展示页面
    @RequestMapping("/showPage")
    public String showPage(Model model){
        return "manager/index";
    }

    //查询全部的用户，并且返回到一个用户展示页面 
    @RequestMapping("/allUser")
    public String userList(Model model){
        List<User> users = userService.queryAllUser();
        model.addAttribute("list",users);
        System.out.println(users.toString());
        return "manager/manager_User";
    }

    @RequestMapping("/allManager")
    public String allManager(Model model){
        List<Manager> managers = managerService.queryAllManager();
        model.addAttribute("managers",managers);
        return "manager/manager_admin";
    }

    //跳转到增加用户页面
    @RequestMapping("/toAddUser")
    public String toAddPaper(Model model){
        return "manager/addUser";
    }

    @RequestMapping("/toAddManager")
    public String toAddManager(Model model){
        return "manager/addManager";
    }

    //添加用户的请求
    @RequestMapping("/addUser")
    public String addUser(User user){
        user.setBeginTime(new Date());
        //System.out.println(user);
        userService.addUser(user);
        return "redirect:/manager/allUser";
    }

    @RequestMapping("/addManager")
    public String addManager(Manager manager){
        manager.setCreateAt(new Date());
        //System.out.println(user);
        managerService.addManager(manager);
        return "redirect:/manager/allManager";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate/{uid}")
    public String toUpdatePaper(@PathVariable("uid") Integer uid,Model model){
        User user = userService.queryUserById(uid);
        model.addAttribute("QUser",user);
        return "manager/updateUser";
    }

    //跳转到修改页面
    @RequestMapping("/toproUpdate/{uid}")
    public String toproUpdate(@PathVariable("uid") Integer uid,Model model){
        User user = userService.queryUserById(uid);
        model.addAttribute("QUser",user);
        return "manager/proUpdate";
    }

    @RequestMapping("/toManagerUpdate/{mid}")
    public String toManagerUpdate(@PathVariable("mid") Integer id,Model model){
        Manager manager = managerService.queryManagerById(id);
        model.addAttribute("Qmanager",manager);
        return "manager/updateManager";
    }

    //修改用户
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        user.setBeginTime(new Date());
        userService.updateUser(user);
        return "redirect:/manager/allUser";
    }

    //修改用户
    @RequestMapping("/updateproUser")
    public String updateproUser(User user,Model model){
        if(userService.queryUserByName(user.getUsername())!=null){
            user=userService.queryUserById(user.getUid());
            System.out.println(user.getUsername()+"修改账号失败，原用户名已存在");
            model.addAttribute("QUser",user);
            model.addAttribute("error","该用户名已存在，请修改为别的账号");
            return "manager/proUpdate";
        }
        else{
            user.setBeginTime(new Date());
            userService.updateUser(user);
            User user1 = userService.queryUserByName(user.getUsername());
            model.addAttribute("Quser",user1);
            return "manager/profile";
        }
    }


    @RequestMapping("/updateManager")
    public String updateManager(Manager manager){
        manager.setCreateAt(new Date());
        managerService.updateManager(manager);
        return "redirect:/manager/allManager";
    }

    //删除用户
    @RequestMapping("/deleteUser/{uid}")
    public String deleteUser(@PathVariable("uid") Integer id){
        userService.deleteUserById(id);
        return "redirect:/manager/allUser";
    }

    @RequestMapping("/deleteManager/{mid}")
    public String deleteManager(@PathVariable("mid") Integer id){
        managerService.deleteManagerById(id);
        return "redirect:/manager/allManager";
    }

    //查询用户
    @RequestMapping("/queryUser")
    public String queryUser(String queryUserName, Model model){
        User user = userService.queryUserByName(queryUserName);
        List<User> list = new ArrayList<>();
        list.add(user);
        model.addAttribute("list",list);
        return "redirect:/manager/allUser";
    }

    //个人展示页面
    @RequestMapping("/showPerson/{username}")
    public String showPerson(@PathVariable("username") String username ,Model model){
        User user = userService.queryUserByName(username);
        System.out.println(user.toString());
        model.addAttribute("Quser",user);
        return "manager/profile";
    }

}
